﻿Ext.regController('ftController', {

    'index': function (options) {

        if (!FlyTalk.views.mainView) {
			//alert('start new main view');
            FlyTalk.views.mainView = new FlyTalk.views.MainView();
        }

        FlyTalk.views.mainView.setActiveItem(
            FlyTalk.views.ModeSelection
        	//FlyTalk.views.NotificationView
        );
    },

	'openItinDetail': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ItinDetailView);
	},
	
/*********************** HOME VIEW ***************************/
	// open search page
	'openSearch': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.SearchView);
    },
    
    'openHome': function (options) {
    	 Ext.dispatch({
             controller: FlyTalk.controllers.dbController,
             action: 'getNewNote',
         });
		//FlyTalk.views.mainView.setActiveItem(FlyTalk.views.HomeView, {type: 'slide', direction: 'left'});
    },
    
    'openReview': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ComposeView);
    },
    
    'openMap': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.MapTab);
    },
    
    'openRestaurant': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.RestaurantView);
    },
    
    'openQuestion': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.QuestionForm);
    },
    
    'openComment': function (options) {
    	FlyTalk.views.QuestionForm.toolbar.setTitle("comment");
    	FlyTalk.views.QuestionForm.items.first().label = "Comment";
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.QuestionForm);
    },
    
    'openReply': function (options) {
    //	alert(options.index);
    	FlyTalk.views.QuestionForm.items.getAt(1).label = options.index;
    	FlyTalk.views.QuestionForm.toolbar.setTitle("Reply");
    	FlyTalk.views.QuestionForm.items.first().label = "Comment";
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.QuestionForm);
    },
    
    'openSuggest': function (options) {
    	FlyTalk.views.mainView.setActiveItem(FlyTalk.views.MapView);
    },
	
	'openMytalk': function (options) {
		//FlyTalk.views.mainView.setActiveItem(FlyTalk.views.MyTalkView);
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.MyTalkView, {type: 'flip', direction: 'right'});
	},
	
	'openProfile': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ProfView);
	},
	

	'openBored': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.BoredView);
	},
	
	'openItinmap': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ItinMapView);
	},
	'closeItinDetailView': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ItinMapView, {type: 'slide', direction: 'right'});
	},
	
	'closeRestaurantView': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ItinMapView, {type: 'slide', direction: 'right'});
	},
	
	'openService': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ServicesView);
	},
	
	'openMap': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.MapView);
	},
	
	'openSettings': function (options) {
		//alert("settings: link later");
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.SettingView, {type: 'slide', direction: 'right'});
	},
	
	// bring up the compose post window
	'openCompose': function (options) {
		alert("need to link to overlay");
		//FlyTalk.views.mainView.setActiveItem(FlyTalk.views.ComposeView, {type: 'slide', direction: 'left'});
    },
	
    // bring up the compose post window
	'openFlytalk': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.TalkView, {type: 'slide', direction: 'left'});
    },
    
    //TODO - CHANGE WORDING
    // open next page
    'openNext': function (options) {
    	
    	if(options.next == 'Air Feed' || options.next == 'Search'){ // in flight talk
			Ext.dispatch({
	            controller: FlyTalk.controllers.dbController,
	            action: 'getMixedPosts',
	            title: 'Air Feed',
	            tab1: 'myFlight',
	            tab2: 'myTerminal',
	            tab3: 'myLanguage',
	            method1: 'getFlightPost',
	            method2: 'getTerminalPost',
	            method3: 'getLangPost',
	            numtabs: 3,
	        });
		}else if(options.next == 'myFeed'){ // my talk
			Ext.dispatch({
		            controller: FlyTalk.controllers.dbController,
		            action: 'getMixedPosts',
		            title: 'myFeed',
		            tab1: 'myTalk',
		            tab2: 'Help Out',
		            method1: 'getMyPost',
		            method2: 'getHelpList', //todo: change this!
		            numtabs: 2,
		    });
		}else if(options.next == 'Bored'){ //bored
			Ext.dispatch({
	            controller: FlyTalk.controllers.dbController,
	            action: 'getMixedPosts',
	            title: 'Bored',
	            tab1: 'Recent Suggestions',
	            tab2: 'Popular Suggestions',
	            method1: 'getRecentBoredPost',
	            method2: 'getPopBoredPost', 
	            numtabs: 2,
	        });
		}else{ // not in talkview (details)
			
		}
    },
    
    
	'openMenu': function (options) {
		// check if the user is already following the post
		var post = FlyTalk.views.ColumnView.MixedStore.getAt(options.index).data;
		
		
//		if(FlyTalk.views.BoredView.opened.title != "opened"){ // only for testing purposes
//			//FlyTalk.views.ColumnView.MixedStore.getAt(0).data.rated = -1;
//			
//		}else{
			// make sure all buttons are shown when starting...
			FlyTalk.views.BoredView.followButton.show();
			FlyTalk.views.BoredView.unfollowButton.show();
			FlyTalk.views.BoredView.upButton.show();
			FlyTalk.views.BoredView.downButton.show();
			FlyTalk.views.BoredView.threadButton.show();
			FlyTalk.views.BoredView.replyButton.show();
			FlyTalk.views.BoredView.askButton.hide();
			FlyTalk.views.BoredView.suggestButton.hide();
			FlyTalk.views.BoredView.reviewButton.hide();
			FlyTalk.views.BoredView.commentButton.hide();
			FlyTalk.views.BoredView.followButton.doComponentLayout();
			FlyTalk.views.BoredView.unfollowButton.doComponentLayout();
			FlyTalk.views.BoredView.upButton.doComponentLayout();
			FlyTalk.views.BoredView.downButton.doComponentLayout();
			FlyTalk.views.BoredView.threadButton.doComponentLayout();
			FlyTalk.views.BoredView.replyButton.doComponentLayout();
			
			// customize follow button
			if(post.followed == 1){ // already follwing a post, then unfollow button shows up
				FlyTalk.views.BoredView.followButton.hide();
			}else{ // not follwing a post, then follow button shows up
				FlyTalk.views.BoredView.unfollowButton.hide();
			}
			
			// customize thumbs up and down buttons
			if(post.rated == 1){   // if already rated, then don't show any thumbs up or down button
				FlyTalk.views.BoredView.upButton.hide();
				FlyTalk.views.BoredView.downButton.hide();
			}
			
			// customize see convo button
			if(post.answered != "yes"){// only display see convo button if the post is a convo
				FlyTalk.views.BoredView.threadButton.hide();
			}
			
			// customize reply button
			//alert("from: "+post.from_uid);
			if(post.from_uid == 3 && FlyTalk.views.MapView.items.first().hidden == true){// only display see convo button if the post is a convo
				FlyTalk.views.BoredView.replyButton.hide();
			}
			
			// pass the index of this variable to the panel
			FlyTalk.views.BoredView.ind.title = options.index;
//		}
		
		// show the overlay when the user click on the + button
		
		setTimeout('FlyTalk.views.BoredView.overlay.show()',5);		
    },
    
    'openPlus': function (options) {
    	FlyTalk.views.BoredView.askButton.show();
		FlyTalk.views.BoredView.suggestButton.show();
		FlyTalk.views.BoredView.reviewButton.show();
		FlyTalk.views.BoredView.commentButton.show();
		FlyTalk.views.BoredView.followButton.hide();
		FlyTalk.views.BoredView.unfollowButton.hide();
		FlyTalk.views.BoredView.upButton.hide();
		FlyTalk.views.BoredView.downButton.hide();
		FlyTalk.views.BoredView.threadButton.hide();
		FlyTalk.views.BoredView.replyButton.hide();
		FlyTalk.views.BoredView.overlay.show();	
		FlyTalk.views.BoredView.doComponentLayout();
		FlyTalk.views.BoredView.suggestButton.doComponentLayout();
		FlyTalk.views.BoredView.askButton.doComponentLayout();
		FlyTalk.views.BoredView.commentButton.doComponentLayout();
		FlyTalk.views.BoredView.reviewButton.doComponentLayout();
    },
    
    'openPlusSuggest': function (options) {
		FlyTalk.views.BoredView.suggestButton.show();
    	FlyTalk.views.BoredView.askButton.hide();
		FlyTalk.views.BoredView.reviewButton.hide();
		FlyTalk.views.BoredView.commentButton.hide();
		FlyTalk.views.BoredView.followButton.hide();
		FlyTalk.views.BoredView.unfollowButton.hide();
		FlyTalk.views.BoredView.upButton.hide();
		FlyTalk.views.BoredView.downButton.hide();
		FlyTalk.views.BoredView.threadButton.hide();
		FlyTalk.views.BoredView.replyButton.hide();
		FlyTalk.views.BoredView.overlay.show();	
		FlyTalk.views.BoredView.suggestButton.doComponentLayout();
    },
	
	
/*********************** SEARCH VIEW ***************************/
	// display tweetDeck
	'toTweetDeck1': function (options) {
		FlyTalk.views.TalkView.setActiveItem(FlyTalk.views.ColumnView, {type: 'slide', direction: 'right'});
    },
	
	// search posts and open a new page
	'searchPosts': function (options) {
		//alert(FlyTalk.views.TalkView.items.length);
		//alert("search ft");
		var newpage = "";
		var index = FlyTalk.views.TalkView.items.length-2;
		// add the view to the app
		if (!FlyTalk.views.searchResultsView) {
		//	alert('create search result 0');
            FlyTalk.views.searchResultsView = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView;	
        }else if(!FlyTalk.views.searchResultsView1){
			//alert('create search result 1');
			FlyTalk.views.searchResultsView1 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView1;
		}else if(!FlyTalk.views.searchResultsView2){
			//alert('create search result 2');
			FlyTalk.views.searchResultsView2 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView2;
		}else if(!FlyTalk.views.searchResultsView3){
			//alert('create search result 3');
			FlyTalk.views.searchResultsView3 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView3;
		}else if(!FlyTalk.views.searchResultsView4){
			//alert('create search result 4');
			FlyTalk.views.searchResultsView4 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView4;
		}else if(!FlyTalk.views.searchResultsView5){
			//alert('create search result 5');
			FlyTalk.views.searchResultsView5 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView5;
		}else if(!FlyTalk.views.searchResultsView6){
			//alert('create search result 6');
			FlyTalk.views.searchResultsView6 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView6;
		}else if(!FlyTalk.views.searchResultsView7){
			//alert('create search result 7');
			FlyTalk.views.searchResultsView7 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView7;
		}else if(!FlyTalk.views.searchResultsView8){
			//alert('create search result 8');
			FlyTalk.views.searchResultsView8 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView8;
		}else if(!FlyTalk.views.searchResultsView9){
			//alert('create search result 9');
			FlyTalk.views.searchResultsView9 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView9;
		}else if(!FlyTalk.views.searchResultsView10){
			//alert('create search result 10');
			FlyTalk.views.searchResultsView10 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView10;
		}else if(!FlyTalk.views.searchResultsView11){
			//alert('create search result 11');
			FlyTalk.views.searchResultsView11 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView11;
		}else if(!FlyTalk.views.searchResultsView12){
			//alert('create search result 12');
			FlyTalk.views.searchResultsView12 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView12;
		}else if(!FlyTalk.views.searchResultsView13){
			//alert('create search result 13');
			FlyTalk.views.searchResultsView13 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView13;
		}else if(!FlyTalk.views.searchResultsView14){
			//alert('create search result 14');
			FlyTalk.views.searchResultsView14 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView14;
		}else if(!FlyTalk.views.searchResultsView15){
			//alert('create search result 15');
			FlyTalk.views.searchResultsView15 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView15;
		}else if(!FlyTalk.views.searchResultsView16){
			//alert('create search result 16');
			FlyTalk.views.searchResultsView16 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView16;
		}else if(!FlyTalk.views.searchResultsView17){
			//alert('create search result 17');
			FlyTalk.views.searchResultsView17 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView17;
		}else if(!FlyTalk.views.searchResultsView18){
			//alert('create search result 18');
			FlyTalk.views.searchResultsView18 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView18;
		}else if(!FlyTalk.views.searchResultsView19){
			//alert('create search result 19');
			FlyTalk.views.searchResultsView19 = new FlyTalk.views.SearchResultsView();
			newpage = FlyTalk.views.searchResultsView19;
		}
	
		// if not at last page, create a new page and display search results
		if(newpage != ""){
			//alert(options.scope);
			// add the search result page to the carousel
			
			// search for the tweets
			FlyTalk.views.TalkView.insert(index,  newpage);
			
			
			var store = new Ext.data.JsonStore({
				model : 'PostModel',
				autoLoad: true,

				proxy: {
          			type: 'scripttag',
          			url: 'http://search.twitter.com/search.json',
          			extraParams: {
             			q: "chair",
                		rpp: 3,
                		//suppress_response_codes: true
          			},
          			reader: {
                		type: 'json',
                		root: 'results',
						totalProperty: 'totalCount' 
         			}
    			},})
				
			/*
			var store = new Ext.data.JsonStore({
				model : 'UserModel',
				autoload: true,
				proxy: {
          			type: 'ajax',
          			url: '/db/dbRetrieveMultiple.php',
          			reader: {
                		type: 'json',
                		root: 'results'
         			}
    			},
			});*/
			//store.load();
			newpage.tweetList.bindStore(store);
			
				
			//alert(store.proxy.reader.rawData.totalCount);
	
			// update TalkView
			FlyTalk.views.TalkView.doLayout();
			
			// display the search result page
			FlyTalk.views.TalkView.setActiveItem(newpage, {type: 'slide', direction: 'right'});
		}else{
				alert("not loading");
		}
    },// end of search posts 1

/*********************** COLUMN VIEW ***************************/
	
	// open thread on a new page
	'openThread': function (options) {
		FlyTalk.views.PostView.tweetPanel.update(options.post);
	//	FlyTalk.views.PostView.doComponentLayout();
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.PostView, {type: 'slide', direction: 'left'});
    },
	
	// open thread on a new page
	'deletePanel': function (options) {
		FlyTalk.views.TalkView.remove(options.panel);
		options.panel.destroy();
		
	//	FlyTalk.views.PostView.doComponentLayout();
		FlyTalk.views.TalkView.setActiveItem(FlyTalk.views.ColumnView, {type: 'slide', direction: 'left'});
    },
	
	
/*********************** POST VIEW ***************************/
	// post msg
	'postMsg': function (options) {
		//alert(options.scope);
		//alert(options.msg);
	
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.TalkView, {type: 'slide', direction: 'right'});
    },
	
	// display tweetDeck
	'toTweetDeck': function (options) {
		FlyTalk.views.mainView.setActiveItem(FlyTalk.views.TalkView, {type: 'slide', direction: 'right'});
    },

});

FlyTalk.controllers.ftController = Ext.ControllerManager.get('ftController');